package com.homni.iomix_xair.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.homni.iomix_xair.R;
import com.homni.iomix_xair.view.CustomProgressGroup;
import com.homni.iomix_xair.view.CustomSeekbarGroup;
import com.homni.iomix_xair.view.PanelView;
import com.homni.iomix_xair.view.VerticalSeekbar;

public class MainActivity extends AppCompatActivity implements PanDialogFragment.PanDialogCallback {

    private CustomSeekbarGroup mCustomSeekbarGroup1;
    private CustomSeekbarGroup mCustomSeekbarGroup2;

    private PanelView mPanelView,mPanelView2;
    private SeekBar mSeekBar;
    private CustomProgressGroup mProgressBar;
    private SeekBar mSeebBar2;
    private Button mPanBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initView(){
        mCustomSeekbarGroup1 = (CustomSeekbarGroup) findViewById(R.id.custom_seekbar_group1);
        mCustomSeekbarGroup2 = (CustomSeekbarGroup) findViewById(R.id.custom_seekbar_group2);

        mCustomSeekbarGroup1.mChannelNoTxt.setText("CH 01");
        mCustomSeekbarGroup2.mChannelNoTxt.setText("CH 02");

        mPanelView = (PanelView) findViewById(R.id.panelView);
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);

        mProgressBar = (CustomProgressGroup) findViewById(R.id.custom_progress_bar);
        mSeebBar2 = (SeekBar) findViewById(R.id.seekBar2);

        mPanBtn = (Button) findViewById(R.id.btn_pan);

    }

    private void initListener(){

        mPanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PanDialogFragment mPanDialog = PanDialogFragment.newInstance(0);
                mPanDialog.show(getSupportFragmentManager(),"");

            }
        });

        mSeebBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i < 101){
                    mProgressBar.mLeftProgressbar.setProgress(100-i);
                    mProgressBar.mRightProgressbar.setProgress(0);
                }else {
                    mProgressBar.mRightProgressbar.setProgress(i-100);
                    mProgressBar.mLeftProgressbar.setProgress(0);
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mPanelView.setPercent(progress);

                mPanelView.setText(progress+"");

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mCustomSeekbarGroup1.mCustomSeekbar.setOnVerticalListener(new VerticalSeekbar.OnVerticalSeekBarListener() {
            @Override
            public void onProgressChanged(VerticalSeekbar vBar, float progress) {
                mCustomSeekbarGroup1.mSeekbarValueTxt.setText(formatProgressValue(progress));

            }

            @Override
            public void onStartTrackingTouch(VerticalSeekbar vBar) {

            }

            @Override
            public void onStopTrackingTouch(VerticalSeekbar vBar, float progress) {

            }
        });

        mCustomSeekbarGroup2.mCustomSeekbar.setOnVerticalListener(new VerticalSeekbar.OnVerticalSeekBarListener() {
            @Override
            public void onProgressChanged(VerticalSeekbar vBar, float progress) {
                mCustomSeekbarGroup2.mSeekbarValueTxt.setText(formatProgressValue(progress));

            }

            @Override
            public void onStartTrackingTouch(VerticalSeekbar vBar) {

            }

            @Override
            public void onStopTrackingTouch(VerticalSeekbar vBar, float progress) {

            }
        });

    }

    /**
     * 格式化seekbar进度值
     * @param value
     * @return
     */
    private String formatProgressValue(float value){
        float valueTemp = (float)10.0 - value ;
        float mValue = (float) (Math.round(valueTemp * 10) / 10.0);
        String mValueString = "";
        if (mValue > 0){
            mValueString = "+" + mValue;
        }else if (mValue == -89.0){
            mValueString = "-∞";
        }else {
            mValueString = String.valueOf(mValue);
        }
        return mValueString;
    }

    @Override
    public void onProgressChangedComplete(int tag, int value) {

    }
}
